<?php


namespace App\Http\Controllers\Mp;


use App\Contract\ContractStatus;
use App\Http\Controllers\Controller;
use App\Http\Resources\PaginationCollection;
use App\Model\Bill;
use App\Model\Contract;
use App\Model\House;
use App\Model\Order;
use App\Model\Repair;
use App\Utils\Result;
use App\Utils\Token;
use App\Model\Device;
use Illuminate\Http\Request;

class TenantController extends Controller
{
    public function contract(Request $request)
    {
        $keyword = $request->input('keyword');

        $user = Token::getUser();

        $query = Contract::query();

        if ($keyword) {
            $query->where(function($q) use ($keyword) {
                $q->whereHas('house', function($q) use ($keyword) {
                    $q->where('estate_name', 'like', "%{$keyword}%")->orWhere('name', 'like', "%{$keyword}%");
                })->orWhereHas('contractTenant', function($q) use ($keyword) {
                    $q->where('name', $keyword);
                });
            });
        }

        $data = $query->whereHas('contractTenant', function($q) use ($user) {
            $q->where('mobile', $user->mobile)->where('status', '>=', ContractStatus::DAIQIANYUE);
        })->whereHas('house')->latest()->with('contractRent', 'house')->get();

        foreach ($data as &$contract) {
            $contract->house->dianfei = $contract->house->dianfei * 1;
        }

        return Result::success($data);
    }

    public function bills(Request $request)
    {
        $page = $request->input('page', 1);

        // Get the number of houses to show per page
        $perPage = 20;

        $status = $request->input('status');
        $type = $request->input('type');
        $contractId = $request->input('contractId');

        $query = Bill::query();
        if ($status == 1) {
            $query->where('is_paid', 0)->orderByDesc('paid_at');
        }
        if ($status == 2) {
            $query->where('is_paid', 1)->orderBy('pay_date');
        }
        if ($status == 3) {
            $query->where('is_paid', 0)->orderBy('pay_date')->where('pay_date', '<', now()->toDateString());
        }
        if ($status == 4) {
            $query->where('is_paid', 1)->orderByDesc('paid_at')
            ->whereHas('billPay', function($q) {
                $q->whereIn('payment', [4, 5]);
            });
        }
        $query->when($type, function($q) use ($type) {
            $q->where('type', $type);
        });
        $query->when($contractId, function($q) use ($contractId) {
            $q->where('contract_id', $contractId);
        });

        $query->where('mobile', Token::getUser()->mobile);
        $data = $query->with('house', 'contract')
            ->orderBy('pay_date')
            ->where('is_close', 0)
            ->whereHas('house')
            ->paginate($perPage, ['*'], 'page', $page);

        return Result::success(new PaginationCollection($data));
    }

    public function repairList(Request $request)
    {
        // Get the current page number from the request parameters (default to 1)
        $page = $request->input('page', 1);
        // Get the number of houses to show per page
        $perPage = 20;

        $query = Repair::query()->where('tenant_id', $this->getTenantId());

        $data = $query->with('tenant', 'house')
            ->orderByDesc('id')
            ->paginate($perPage, ['*'], 'page', $page);

        return Result::success(new PaginationCollection($data));
    }

    public function repairCreate(Request $request)
    {
        $house = House::find($request->input('house_id'));
        $res = new Repair();
        $res->tenant_id = $this->getTenantId();
        $res->business_id = $house->business_id;
        $res->fill($request->all());
        $res->save();

        return Result::success($res);
    }

    public function repairConfirm($id)
    {
        $res = Repair::find($id);
        $res->status = 2;
        $res->save();

        return Result::success($res);
    }

    public function deviceList(Request $request)
    {
        $keyword = $request->input('keyword');

        $houseId = $request->input('houseId');

        $query = Device::query();
        $query->when($keyword, function($q) use ($keyword) {
            $q->whereHas('house', function($q) use ($keyword) {
                $q->where('estate_name', 'like', "%{$keyword}%")->orWhere('name', 'like', "%{$keyword}%");
            })->orWhere('sn', $keyword);
        });
        $query->when($houseId, function($q) use($houseId) {
            $q->where('house_id', $houseId);
        });

        $data = $query->with('house')
            ->orderByDesc('id')
            ->get();

        return Result::success($data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function orders(Request $request)
    {
        $page = $request->input('page', 1);
        // Get the number of houses to show per page
        $perPage = 20;
        $query = Order::query();
        $query->where('user_id', Token::getUser()->id);
        $data = $query->orderByDesc('paid_at')
            ->with('user')
            ->with('business')
            ->with('business.agent')
            ->with('house')
            ->with('device')
            ->where('status', 3)
            ->whereHas('house')
            ->paginate($perPage, ['*'], 'page', $page);
        return Result::success(new PaginationCollection($data));
    }
}
